<template>
    <div class="search-box">
    <div class="location">
      <div class="city" @click="cityClick">{{ cityStore.currentcity?.cityName || '广州'}}</div>
      <div class="address" @click="getaddress">
        <span class="text">我的位置</span>
        <img src="../../assets/img/home/icon_location.png" alt="">
    </div>
    </div>
    <div class="section date-range">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ mainstore.startDate  }}</span>
        </div>
        <div class="stay" @click="changeshowcalendar">共{{mainstore.diffdays}}晚</div>
      </div>
      
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ mainstore.endDate }}</span>
        </div>
      </div>
    </div>
    <van-calendar v-model:show="showCalendar" 
    type="range"
     @confirm="onConfirm" />


    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>

    <div class="section keyword bottom-gray-line" >关键字/位置/民宿</div>
    <div class="section hot-suggests">
      <template v-for="(item, index) in hotsuggests" :key="index">
         <div class="tag">{{ item.tagText.text }}</div>
      </template>
    </div>

    <div class="section search-btn">
      <div class="btn" @click="searchbtn">开始搜索</div>
    </div>
    </div>
</template>

<script setup>
import usecityStore from '@/stores/modules/city.js';
import { formatMonthDay ,formatdiffdays} from '@/utils/format_date';
import { storeToRefs } from 'pinia';
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import useHomeStore from '@/stores/modules/home.js'
import useMainStore from '@/stores/modules/main.js'
import Home from '../home.vue';

const router=useRouter();

// defineProps({
//   hotSuggests:{
//     type:Array,
//     default:()=>[],
//   }
// })
const cityStore=usecityStore(); 
const HomeStore=useHomeStore();
// console.log(cityStore.currentcity);
// console.log(HomeStore.hotSuggests);



  const getaddress=()=>{
    navigator.geolocation.getCurrentPosition(res=>{
      console.log("得到正确的位置：",res); 
    },err=>{
      console.log("error");      
    })
  }
  const cityClick=()=>{
    console.log("点击了城市");
    router.push('/city')
  }
  
const mainstore=useMainStore();
  //日期处理
  const today=new Date();
  const startday=ref(formatMonthDay(today));
  const newday=new Date().setDate(today.getDate()+2);
  const endday=ref(formatMonthDay(newday));

  const showCalendar=ref(false);
  const changeshowcalendar=()=>{
    showCalendar.value=true;
  }
  const onConfirm=(value)=>{
    diffdays.value=formatdiffdays(value[0],value[1]);
    // console.log("value",value);
    startday.value=formatMonthDay(value[0]);
    endday.value=formatMonthDay(value[1]);
    // console.log(startday.value,endday.value);
    
    mainstore.startDate=startday.value;
    mainstore.endDate=endday.value;
    mainstore.diffdays=diffdays.value;
   
    showCalendar.value=false;

  }
  const diffdays=ref(formatdiffdays(today,newday));
  HomeStore.fetchHotSuggests();
  HomeStore.fetchCategories();
  const {hotsuggests}=HomeStore;
  console.log(hotsuggests);
  const {categories} =HomeStore;
  console.log(categories);
  

  const searchbtn=()=>{
    router.push({
      path:"/search",
      query:{
        startday:startday.value,
        endday:endday.value,
       currentcity:cityStore.currentcity?.cityName || '广州',
      }
    });
  }
  
</script>

<style lang="less" scoped>
.location{
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;
  .city{
    flex:1;
    font-size: 14px;
  }

  .address{
    width: 74px;
    display: flex;
    align-items: center;
    .text{
      position: relative;
      top: 2px;
      color: #666;
      font-size: 12px;
    }
    img{
      margin: 0 4px;
      width: 18px;height: 18px;
    }
  }
}
.search-box{
  --van-calendar-popup-height:100%;
}
.date-range {
  // width: 100%;
  height: 44px;
  .stay {
    // margin-left: 80px;
    text-align: center;
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
 
  
  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
    // padding-left: 20px;
    // text-align: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
    text-align: center;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999;
    }

    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}
.price-counter {
  height: 44px;
  .start {
    border-right: 1px solid f8f9f9;
  }
}

.keyword {
  height: 44px;
}
.bottom-gray-line{
  border: 0.001px solid #e6e4e4;
}
.hot-suggests {
  margin: 10px 0;
  height: auto;
  .tag {
    font-size: 12px;
    padding: 4px 8px;
    margin: 3px;
    border-radius: 14px;
    color: #3f4954;
    background-color: #f1f3f5;
  }
}
.search-btn {
  
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-image:  linear-gradient(90deg, #fa8c1d, #fcaf3f)
  }
}

</style>